package com.allwees.bs.module.pms.vo.excel;

import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import com.opencsv.bean.CsvBindByName;
import com.allwees.bs.module.pms.entity.Sku;
import lombok.Data;
import lombok.EqualsAndHashCode;
import me.zhengjie.utils.StringUtils;

import java.math.BigDecimal;

/**
 * 一步导入Product和Sku（参考Wish模板）
 *
 * @author yangylsky
 * @since 2020-03-03
 */
@Data
@EqualsAndHashCode(callSuper = false)
public class ImportWishProductSkuBean extends ImportProductSkuBean {

    @ExcelProperty("Product ID")
    @CsvBindByName(column = "Product ID")
    protected String productCustomNo;

    @ExcelProperty("Product Name")
    @CsvBindByName(column = "Product Name")
    protected String productName;

    @ExcelProperty("Description")
    @CsvBindByName(column = "Description")
    protected String description;

    @ExcelProperty("Variation ID")
    @CsvBindByName(column = "Variation ID")
    protected String skuUuid;

    @ExcelProperty("SKU")
    @CsvBindByName(column = "SKU")
    protected String skuCustomNo;

    @ExcelProperty("Size")
    @CsvBindByName(column = "Size")
    protected String size;

    @ExcelProperty("Color")
    @CsvBindByName(column = "Color")
    protected String color;

    @ExcelProperty("Cost")
    @CsvBindByName(column = "Cost", capture = "\\$((([1-9]\\d*)|([0]))(\\.(\\d)*))\\s*")
    protected BigDecimal retailPrice = BigDecimal.ZERO;

    @ExcelProperty("Price")
    @CsvBindByName(column = "Price", capture = "\\$((([1-9]\\d*)|([0]))(\\.(\\d)*))\\s*")
    protected BigDecimal originalPrice = BigDecimal.ZERO;

    @ExcelProperty("Shipping")
    @CsvBindByName(column = "Shipping", capture = "\\$((([1-9]\\d*)|([0]))(\\.(\\d)*))\\s*")
    protected BigDecimal shippingPrice = BigDecimal.ZERO;

    @ExcelProperty("Inventory(库存 ）")
    @CsvBindByName(column = "Inventory(库存 ）")
    protected Integer quantity;

    @ExcelProperty("Shipping Time")
    @CsvBindByName(column = "Shipping Time")
    protected String shippingArrivalDesc;

    @ExcelProperty("Image URL")
    @CsvBindByName(column = "Image URL")
    protected String mainPhoto;

    @ExcelProperty("Extra Images")
    @CsvBindByName(column = "Extra Images")
    protected String extraImages;

    @ExcelProperty("Tags")
    @CsvBindByName(column = "Tags")
    protected String keywords;

    @ExcelIgnore
    protected String currency = "USD";

    @ExcelIgnore
    protected String shippingCurrency = "USD";

    @ExcelIgnore
    protected String categoryName = "";

    @ExcelIgnore
    protected String categoryZh = "";

    @ExcelIgnore
    protected Integer shippingMinDays;

    @ExcelIgnore
    protected Integer shippingMaxDays;

    @ExcelIgnore
    protected Float weight;

    @ExcelIgnore
    protected String weightUnit;

    @Override
    public Sku toSku() {
        Sku sku = super.toSku();
        sku.setCustomNo(this.getSkuUuid());
        sku.setNo(getSkuUuid());
        return sku;
    }

    @Override
    public String getShippingArrivalDesc() {
        if (StringUtils.isNotBlank(shippingArrivalDesc)) {
            shippingArrivalDesc = shippingArrivalDesc.replace("\"", "").replace("月", "-").replace("日", "");
        }
        return shippingArrivalDesc;
    }

    @Override
    public void initData() {
        if (extraImages != null) {
            String[] photos = extraImages.split("\\|");
            for (String photo : photos) {
                addExtraPhoto(photo);
            }
        }

        if (keywords != null) {
            //解析字符串 id:deepvneck,name:Deep V-Neck|id:slimfit,name:Slim Fit
            StringBuilder keywords_sb = new StringBuilder();
            String[] keywords_arr = keywords.split("\\|");
            for (String s : keywords_arr) {
                String keywords_item = s.split(",")[0];
                keywords_sb.append(keywords_item.replace("id:", "")).append(",");
            }
            keywords = keywords_sb.toString();
            if (keywords.endsWith(",")) {
                keywords = keywords.substring(0, keywords.length() - 1);
            }
        }
    }
}
